﻿using System;
using System.Collections.Generic;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.GenericMetadata;
using System.Text;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta
{
    public class CodelistProvider : BaseMetaProvider, IMetaProvider
    {
        private CodeListType clt;

        public CodelistProvider(string lang, string metaId, BookConfiguration cfg)
            : base(lang, metaId, cfg)
        {
             try
            {
                // Get required maintenable artefact
                ArtefactRefTyped art = new ArtefactRefTyped(SdmxArtefactType.CodeLists, fullIdKey);
                sm.LoadMissingArtefactsFromRegistry(art);
                clt = sm.GetArtefact(art) as CodeListType;

                ProduceHierarchy(clt);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to obtain related metadata for " + fullIdKey, ex);
            }
        }

        private void ProduceHierarchy(CodeListType clt)
        {
            hierarchy = new Hierarchy();
            HierarchyNode root = ProduceCodelistHierarchy(clt, false);
            hierarchy.Add(root);
        }

    }
}
